package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.activity.VideoPlayActivity;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.RecommendVideo;
import com.example.myapplication.entity.RelatedVideo;
import com.example.myapplication.entity.VideoDetail;
import com.example.myapplication.entity.VideoUrl;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VideoPlayRecyclerViewAdapter extends RecyclerView.Adapter<VideoPlayRecyclerViewAdapter.VideoPlayRecyclerViewHolder>{
    private Context context;
    private List<RelatedVideo.DataBean> datasBeanList;
    private RecommendVideo.DatasBean datasBean = new RecommendVideo.DatasBean();
    private RecommendVideo.DatasBean.DataBean data1Bean = new RecommendVideo.DatasBean.DataBean();
    private Intent intent;
    private Bundle bundle;

    public void setData(List<RelatedVideo.DataBean> datasBeanList){
        this.datasBeanList = datasBeanList;
        notifyDataSetChanged();
    }
    public VideoPlayRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public VideoPlayRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_play_related,parent,false);
        return new VideoPlayRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoPlayRecyclerViewHolder holder, int position) {
        RelatedVideo.DataBean dataBean = datasBeanList.get(position);
        holder.name_related.setText(dataBean.getTitle());
        holder.desc_related.setText("来自 "+dataBean.getCreator().get(0).getUserName());
        String time = formatLongToTimeStr(dataBean.getDurationms());
        holder.duration_time.setText(time);
        holder.play_time.setText(dataBean.getPlayTime()/10000+"万次播放");

        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(dataBean.getCoverUrl())
                .centerCrop().apply(options).into(holder.img_related);

        holder.card_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(context,VideoPlayActivity.class);
                bundle = new Bundle();
                Toast.makeText(context, "加载中,请稍后..", Toast.LENGTH_SHORT).show();
                loadData(dataBean.getVid());
            }
        });

    }

    @Override
    public int getItemCount() {
        return datasBeanList==null?0:datasBeanList.size();
    }

    public static String formatLongToTimeStr(int l) {
        String str = "";
        int hour = 0;
        int minute = 0;
        int second = 0;

        second = l/ 1000;

        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        return (hour + ":" + minute  + ":"
                + second);
    }

    private void loadData(String id){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://autumnfish.cn/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            MusicApiService service =  retrofit.create(MusicApiService.class);

            Call<VideoDetail> call = service.getVideoDetail(id);
            call.enqueue(new Callback<VideoDetail>() {
                @Override
                public void onResponse(Call<VideoDetail> call, Response<VideoDetail> response) {
                    VideoDetail baseResponse = response.body();
                    data1Bean.setVid(baseResponse.getData().getVid());
                    data1Bean.setTitle(baseResponse.getData().getTitle());
                    RecommendVideo.DatasBean.DataBean.CreatorBean creatorBean = new RecommendVideo.DatasBean.DataBean.CreatorBean();
                    creatorBean.setNickname(baseResponse.getData().getCreator().getNickname());
                    creatorBean.setUserType(baseResponse.getData().getCreator().getUserType());
                    creatorBean.setAvatarUrl(baseResponse.getData().getCreator().getAvatarUrl());
                    data1Bean.setCreator(creatorBean);
                    loadDataUrl(id);
                }

                @Override
                public void onFailure(Call<VideoDetail> call, Throwable t) {
                    t.printStackTrace();
                }
            });

    }
    private void loadDataUrl(String id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<VideoUrl> call = service.getVideoUrl(id);
        call.enqueue(new Callback<VideoUrl>() {
            @Override
            public void onResponse(Call<VideoUrl> call, Response<VideoUrl> response) {
                VideoUrl baseResponse = response.body();
                RecommendVideo.DatasBean.DataBean.UrlInfoBean infoBean = new RecommendVideo.DatasBean.DataBean.UrlInfoBean();
                infoBean.setUrl(baseResponse.getUrls().get(0).getUrl());
                infoBean.setId(baseResponse.getUrls().get(0).getId());
                data1Bean.setUrlInfo(infoBean);
                datasBean.setData(data1Bean);
                bundle.putSerializable("video",datasBean);
                intent.putExtras(bundle);
                context.startActivity(intent);

            }

            @Override
            public void onFailure(Call<VideoUrl> call, Throwable t) {
                t.printStackTrace();
            }
        });

    }



    class VideoPlayRecyclerViewHolder extends RecyclerView.ViewHolder{
        TextView name_related;
        TextView desc_related;
        TextView play_time;
        TextView duration_time;
        ImageView img_related;
        View card_view;
        public VideoPlayRecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
            name_related=itemView.findViewById(R.id.name_related);
            desc_related=itemView.findViewById(R.id.desc_related);
            play_time=itemView.findViewById(R.id.play_time);
            duration_time=itemView.findViewById(R.id.duration_time);
            img_related=itemView.findViewById(R.id.img_related);
            card_view = itemView.findViewById(R.id.card_view);
        }
    }
}
